package primalgorithms.tree;

public class _03IsSymmetric {
    public static boolean isSymmetric(TreeNode root) {
        //递归算法
        //这道题中，每次需要比较的树始终只有两个！
        if (root == null){
            return true;
        }
        return leftAndRight(root.left,root.right);
    }

    private static boolean leftAndRight(TreeNode left, TreeNode right) {
        if (left == null && right == null){
            return true;
        }
        //执行到此处说明子树至少存在一个
        if (left == null || right == null || left.val != right.val){
            return false;
        }
        return leftAndRight(left.left,right.right) && leftAndRight(left.right,right.left);
    }
}
